
package com.example.controller;

import com.example.service.EthService;
import com.example.common.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

import io.swagger.v3.oas.annotations.tags.Tag;
/**
 * 以太坊链基础接口
 */
@RestController
@RequestMapping("/api/eth")
@Tag(name = "ETH管理", description = "以太坊链基础功能接口")
public class EthController {

    @Operation(summary = "查询ETH余额")
    @GetMapping("/eth-balance")
    public ApiResult<java.math.BigInteger> getEthBalance(@org.springframework.web.bind.annotation.RequestParam String address) {
        try {
            java.math.BigInteger balance = ethService.getEthBalance(address);
            return ApiResult.success(balance);
        } catch (Exception e) {
            return ApiResult.error(500, "查询ETH余额失败: " + e.getMessage());
        }
    }

    @Operation(summary = "ETH 转账")
    @org.springframework.web.bind.annotation.PostMapping("/eth-transfer")
    public ApiResult<String> transferEth(@org.springframework.web.bind.annotation.RequestParam String to,
                                         @org.springframework.web.bind.annotation.RequestParam java.math.BigInteger amount) {
        try {
            String txHash = ethService.transferEth(to, amount);
            return ApiResult.success(txHash);
        } catch (Exception e) {
            return ApiResult.error(500, "ETH转账失败: " + e.getMessage());
        }
    }
    @Resource
    private EthService ethService;

    @Operation(summary = "获取最新区块号")
    @GetMapping("/block-number")
    public ApiResult<java.util.Map<String, Object>> getBlockNumber() {
        try {
            Long blockNumber = ethService.getBlockNumber();
            // 用匿名内部类或Map包装返回对象
            java.util.Map<String, Object> result = new java.util.HashMap<>();
            result.put("blockNumber", blockNumber);
            return ApiResult.success(result);
        } catch (Exception e) {
            return ApiResult.error(500, "获取区块号失败: " + e.getMessage());
        }
    }
}
